package com.cloudpivot.receivales.contract.service.impl;

import com.cloudpivot.receivales.contract.entity.erpcontract.*;
import com.cloudpivot.receivales.contract.repository.erpcontract.*;
import com.cloudpivot.receivales.contract.service.ErpContractService;
import com.cloudpivot.receivales.contract.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

import static com.cloudpivot.receivales.contract.util.StrUtil.getDateNow;
import static com.cloudpivot.receivales.contract.util.StrUtil.newGuid;

/**
 * erp合同服务实现类
 */
@Service
@Slf4j
public class ErpContractServiceImpl implements ErpContractService {

    @Autowired
    private LastContractIdRepository lastContractIdRepository;

    @Autowired
    private CMContractMainRepository CMContractMainRepository;

    @Autowired
    private CMListRepository cmListRepository;

    @Autowired
    private CMContractBRepository cmContractBRepository;

    @Autowired
    private CMContractBExtradefineRepository cmContractBExtradefineRepository;

    @Autowired
    private CMContractItemBRepository cmContractItemBRepository;

    @Autowired
    private CMContractItemBExtradeineRepository cmContractItemBExtradeineRepository;


    /**
     * 更新存放位置 erp
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateCFWZ(String cDefine13,String contractId) {
        cmContractBRepository.updatecDefine13(cDefine13,contractId);
    }


    /**
     * 测试用事务 服务
     */
    @Transactional(rollbackFor = Exception.class)
    public void rollback(){
        String strContractId="test1";
        CMContractMain cmContractMain =new CMContractMain();
        cmContractMain.setStrContractID(strContractId);

        CMList cmList=new CMList();
        cmList.setGuid(newGuid());
        cmList.setDtDate(getDateNow());
        cmList.setStrContractID(strContractId);
        try {
            cmListRepository.save(cmList);
            CMContractMainRepository.addCMContractMain(strContractId);
        }catch (Exception ex){
            ex.printStackTrace();
            log.info("插入出现异常,全部数据回滚");
        }
    }

    /**
     * 新增cmcontractitembextradeine 合同子表 扩展项
     * @param cmContractItemBExtradefines
     */
    @Override
    public void addCMContractItemBExtradefine(List<CMContractItemBExtradefine> cmContractItemBExtradefines) {
        cmContractItemBExtradeineRepository.saveAll(cmContractItemBExtradefines);
        log.info("插入合同明细表子项扩展项成功");
    }

    /**
     * 新增cmcontractItemb 合同子表记录
     * @param cmContractItemBs
     */
    @Override
    public void addCMContractItemB(List<CMContractItemB> cmContractItemBs) {
        cmContractItemBRepository.saveAll(cmContractItemBs);
        log.info("插入合同明细表子项成功");
    }

    /**
     * 新增cmcontractbextradefine 合同主表扩展项
     */
    @Override
    public void addCMContractBExtradeine(CMContractBExtradefine cmContractBExtradefine) {
        cmContractBExtradefineRepository.save(cmContractBExtradefine);
        log.info("插入合同主表扩展项成功");
    }

    /**
     * 新增cmContractB记录--合同主表
     */
    @Override
    public void addCMContractB(CMContractB cmContractB){

        cmContractBRepository.save(cmContractB);
        log.info("插入合同主表成功(CMContractB)");
    }

    /**
     * 新增cmlist记录
     * @param cmlist
     */
    @Override
    public void addCMList(CMList cmlist) {
        int resultInt=cmListRepository.addCMList(cmlist);
        log.info("成功插入记录:"+resultInt);

    }

    /**
     * 新增cmcontractmain记录 只需增加strContractId
     *
     */
    @Override
    public void addCMContractMain(String strContractId) {
        CMContractMain cmContractMain =new CMContractMain();
        cmContractMain.setStrContractID(strContractId);
        int cm=CMContractMainRepository.addCMContractMain(strContractId);
        log.info("插入新记录(cm_list):"+cm);
    }

    /**
     * 获取账套下,最后一个id 如果获取不到为-100
     * @return
     */
    @Override
    public String getLastContractId() {

        LastContractId lastContractId= lastContractIdRepository.getZJBSLastContractId();
        log.info("数据库最后一个合同号为{}", lastContractId);
        //获取最后一个合同id号
        String lastId = StrUtil.contractIdAdd(lastContractId.getStrContractID());
        log.info("系统获取到最后一个可用的合同编码为：{}", lastId);
        return lastId;

    }





}
